// import { fromJS } from 'immutable';
import * as constants from './constants';

const defaultState = {
    appList: [],
    devicesList: [],
    allImgList: [],
    appDeviceImgList: [],
    appExecTimeList: [],
    currentAppInfo: {},
    selectAppImgItem: '',
    nextDayExecTime: {},
    execHealthInfo: [],
    serviceHost: 'error',
};

export default (state = defaultState, action) => {
    switch (action.type) {
        case constants.REQ_APP_LIST:
            return {...state, 'appList': action.appList};
        case constants.REQ_APP_DEVICE_IMG_LIST:
            return {...state, 'appDeviceImgList': action.appDeviceImgList};
        case constants.REQ_DEVICES_LIST:
            return {...state, 'devicesList': action.devicesList};
        case constants.REQ_ALL_IMG_LIST:
            let tmp = state.allImgList;
            tmp.unshift(action.allImgList);

            let device = action.allImgList.split('/')[3];
            let tmpDevice = state[`rym_${device}`];
            if (tmpDevice == null) {
                tmpDevice = [];
            }
            tmpDevice.unshift(action.allImgList);
            // 设备号可能是数字开头，做属性需加前缀
            return {...state, 'allImgList': tmp, [`rym_${device}`]: tmpDevice};
        case constants.REQ_APP_EXEC_TIME_LIST:
            return {...state, 'appExecTimeList': action.appExecTimeList};
        case constants.REQ_CURRENT_APP_INFO:
            return {...state, 'currentAppInfo': action.currentAppInfo};
        case constants.REQ_SELECT_APP_IMG:
            return {...state, 'selectAppImgItem': action.selectAppImgItem};
        case constants.REQ_NEXT_DAY_EXEC_TIME:
            return {...state, 'nextDayExecTime': action.nextDayExecTime};
        case constants.REQ_EXEC_HEALTH_INFO:
            return {...state, 'execHealthInfo': action.execHealthInfo};
        case constants.REQ_APP_SERVICE_HOST:
            return {...state, 'serviceHost': action.serviceHost};
        default:
            return state;
    }
}
